package com.api.fc;
//会议归档写建模的api

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.upgradetool.wscheck.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/fc")
public class fc_get_khmc {
    //    private Logger logger = LoggerFactory.getLogger(Hygdxjm.class);
    private RecordSet rs = new RecordSet();

    @POST
    @Path("/get_sy_khmc")
    @Produces(MediaType.APPLICATION_JSON)
    public String get_sy_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject errorobj = new JSONObject();
        RecordSet rs = new RecordSet();
        String id = "";//111
        String khmc = "";//
        String fanhui="";
        String sql = "select id,khmc from uf_KHMC  ";
        rs.executeSql(sql);
        while(rs.next()){
            id = Util.null2String(rs.getString("id"));
            khmc = Util.null2String(rs.getString("khmc"));
            String line="{id:"+id+",khmc:"+khmc+"}";
            if(fanhui.equals("")){
                fanhui+=line;
            }else{
                fanhui+=","+line;
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("cust",fanhui);
        String json = JSONObject.toJSONString(data);
        return json;
    }

    @POST
    @Path("/add_sy_khmc")
    @Produces(MediaType.APPLICATION_JSON)
    public String add_sy_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String khmc=request.getParameter("khmc");
        String sfsyz=request.getParameter("sfsyz");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
        String result="";

        RecordSet rs = new RecordSet();
        String sql = "insert into uf_khmc (KHMC,sfsyz,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime)values" +
                "('"+khmc+"','"+sfsyz+"','2','243','0','"+sdf1.format(new Date())+"','"+sdf2.format(new Date())+"')";
        if("".equals(khmc)||khmc==null||"".equals(sfsyz)||sfsyz==null){
            result="参数不全";
        }else{
            boolean tt=rs.executeUpdate(sql);
            if(tt==true){
                result="true";
            }else{
                result="false";
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",result);
        String json = JSONObject.toJSONString(data);
        return json;
    }

    @POST
    @Path("/add_sy_khmc_list")
    @Produces(MediaType.APPLICATION_JSON)
    public String add_sy_khmc_list(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String listparam=request.getParameter("listparam");
        String result="";
        Object object_listparam = JSONObject.parse(listparam);

        List<HashMap<String, Object>> maplist =  (List<HashMap<String, Object>>) object_listparam;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

        String sql = "insert into uf_khmc (KHMC,sfsyz,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime)values ";
        String sql_add="";
        if(maplist.size()>100){
            result="请分批调用每批最多100条";
        }else{
            for( int i=0;i<maplist.size();i++){
                Object oo = JSON.parse(JSONObject.toJSONString(maplist.get(i)));
                Map mapoo = (Map)oo;         //Object强转换为Map
                String khmc=(String) mapoo.get("khmc");
                String sfsyz=(String) mapoo.get("sfsyz");
                if("".equals(khmc)||khmc==null||"".equals(sfsyz)||sfsyz==null){
                    result="参数不全";break;
                }else{
                    if("".equals(sql_add)){
                        sql_add+="('"+khmc+"','"+sfsyz+"','2','243','0','"+sdf1.format(new Date())+"','"+sdf2.format(new Date())+"')";
                    }else{
                        sql_add+=",('"+khmc+"','"+sfsyz+"','2','243','0','"+sdf1.format(new Date())+"','"+sdf2.format(new Date())+"')";
                    }
                }
            }
        }
        RecordSet rs = new RecordSet();
        if("".equals(result)){
            sql=sql+sql_add;
            boolean tt=rs.executeUpdate(sql);
            if(tt==true){
                result="true";
            }else{
                result="false";
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",result);
        String json = JSONObject.toJSONString(data);
        return json;
    }
    @POST
    @Path("/del_sy_khmc")
    @Produces(MediaType.APPLICATION_JSON)
    public String del_sy_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String id=request.getParameter("id");
        String result="";
        RecordSet rs = new RecordSet();
        String sql = "delete from  uf_khmc where  id="+id;
        if("".equals(id)||id==null){
            result="参数不全";
        }else{
            boolean tt=rs.executeUpdate(sql);
            if(tt==true){
                result="true";
            }else{
                result="false";
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",result);
        String json = JSONObject.toJSONString(data);
        return json;
    }

    @POST
    @Path("/update_sy_khmc")
    @Produces(MediaType.APPLICATION_JSON)
    public String update_sy_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String sfsyz=request.getParameter("sfsyz");
        String id=request.getParameter("id");
        String result="";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        RecordSet rs = new RecordSet();
        String fanhui="";
        String sql = "update  uf_khmc set sfsyz='"+sfsyz+"',modedatamodifydatetime='"+sdf1.format(new Date())+"',modedatamodifier=243    where id ="+id;
        if("".equals(id)||id==null||"".equals(sfsyz)||sfsyz==null){
            result="参数不全";
        }else{
            boolean tt=rs.executeUpdate(sql);
            if(tt==true){
                result="true";
            }else{
                result="false";
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("result",result);
        String json = JSONObject.toJSONString(data);
        return json;
    }

    @POST
    @Path("/get_zk_khmc")
    @Produces(MediaType.APPLICATION_JSON)
    public String get_zk_khmc(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject errorobj = new JSONObject();
        RecordSet rs = new RecordSet();
        String id = "";//
        String khmc = "";//
        String fanhui="";
        String sql = "select id,zkkhmc khmc from uf_zkkhmc ";
        rs.executeSql(sql);
        while(rs.next()){
            id = Util.null2String(rs.getString("id"));
            khmc = Util.null2String(rs.getString("khmc"));
            String line="{id:"+id+",khmc:"+khmc+"}";
            if(fanhui.equals("")){
                fanhui+=line;
            }else{
                fanhui+=","+line;
            }
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("cust",fanhui);
        String json = JSONObject.toJSONString(data);
        return json;

    }

}